<?php
/**
 * 用户矿机
 */
namespace app\api\model;

use app\common\model\MemberMining as MemberMiningModel;
use app\common\model\Mining as MiningModel;
use app\api\model\MiningSpecification as MiningSpecificationModel;

class MemberMining extends MemberMiningModel
{
    /**
     * 统计矿机套餐
     * @param $mid  用户ID
     * @param $msid 矿机ID
     * @param array $where  查询条件
     * @return int
     */
    public static function countMining($mid,$msid,$where=[]){
        $data = self::where(['mid'=>$mid,'msid'=>$msid]);
        if(!empty($where)) $data = $data->where($where);
        return $data->count();
    }


    /**
     * 查询用户某一个矿机详情
     * @param $mid  用户ID
     * @param $min_id   矿机ID
     * @return MemberMining|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function user_Details($mid,$min_id){
        return self::where('mid',$mid)->where('min_id',$min_id)->find();
    }

    /**
     * 添加和修改用户矿机购入值
     * @param $mid  用户ID
     * @param $min_id   矿机ID
     * @param $num  购入数量
     * @param $money  支付金额
     * @return int|mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function update_mining($mid,$min_id,$num,$money){
        //查询该用户矿机详情
        $member_mining = self::user_Details($mid,$min_id);
        if(empty($member_mining)){  //没有该矿机
            $data = [
                'mid'   =>  $mid,
                'min_id'=>  $min_id,
                'pay_total' =>  $num,
                'pay_price' =>  $money,
                'add_time'  =>  date('Y-m-d H:i:s',time()),
                'update_time'  =>  date('Y-m-d H:i:s',time()),
            ];
            $id = self::insertGetId($data);

        }else{  //更改矿机购入数量
            $data['pay_total'] = bcadd($member_mining['pay_total'],$num);
            $data['pay_price'] = bcadd($member_mining['pay_price'],$money);
            self::where('id',$member_mining['id'])->update($data);
            $id = $member_mining['id'];
        }
        return $id;
    }

    /**
     * 用户矿机列表
     * @param $mid
     * @param $page
     * @param $limit
     * @param string $order
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getMemberList($mid,$page,$limit,$order = 'id desc',$language = ''){
        $data = self::where('mid',$mid)->field('id,miid,msid,num,money,output,day,endtime,status,add_time')->order($order)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
        foreach ($data['data'] as $k => $v){

            $field = file_language('name',$language).','.file_language('label',$language).','.file_language('pic',$language).',status,duration,daily_release';
            $MiningSpecification = MiningSpecificationModel::getDetails($v['msid'],$field);
            $mining = MiningModel::getDetails($v['miid']);

            unset($data['data'][$k]['id']);
            unset($data['data'][$k]['msid']);
            unset($data['data'][$k]['miid']);
            $data['data'][$k]['equipment'] =    $mining['equipment'];
            $data['data'][$k]['chip'] = $mining['chip'];

            $data['data'][$k]['mining'] = [];
            if(!empty($MiningSpecification)){
                $data['data'][$k]['mining'] = [
                    'name'  =>  $MiningSpecification['name'],
                    'label'  =>  $MiningSpecification['label'],
                    'pic'  =>  $MiningSpecification['pic'],
                    'status'  =>  $MiningSpecification['status'],
                    'duration'  =>  $MiningSpecification['duration'],    //有效日期(天)
                    'daily_release'  =>  $MiningSpecification['daily_release'],  //每日释放
                ];
            }
        }
        return $data;
    }

    /**
     * 用户购买矿机
     * @param $mid  用户ID
     * @param $mining 矿机套餐详情
     * @param $num  购买数量
     * @param $money    购买金额
     */
    public static function payMining($mid,$mining,$num,$money){
        $data = [
            'mid'   =>  $mid,
            'msid'  =>  $mining['id'],  //套餐ID
            'miid'  =>  $mining['pid'], //矿机ID
            'num'   =>  $num,
            'money' =>  $money,
            'endtime'   => $mining['duration'],
            'status'    =>  1,
            'add_time'   => date('Y-m-d H:i:s',time()),
            'update_time'   =>  date('Y-m-d H:i:s',time())
        ];
        return self::insertGetId($data);
    }
    
    /**
     * 统计用户矿机已产出
     * @param $mid 用户ID
     * @param int $miid 矿机ID 0为所有矿机
     * @return float
     */
    public static function total_income($mid,$miid = 0){
        $data = self::where('mid',$mid);
        if($miid > 0) $data = $data->where('miid',$miid);
        return $data->sum('output');
    }

}